package edu.yctc.manage.controller.rest;

import edu.yctc.manage.entity.PageDO;
import edu.yctc.manage.entity.ResultDO;
import edu.yctc.manage.entity.SdeptDO;
import edu.yctc.manage.service.SdeptService;
import edu.yctc.manage.utils.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName SdeptRestController
 * @Description TODO
 * @Author zyj
 * @Date 2021/3/30 10:47
 * @Version 1.0
 **/
@RestController
@RequestMapping("manage/sdept")
public class SdeptRestController {

    @Autowired
    private SdeptService sdeptService;

    @PostMapping("get-sdept-list")
    ResultDO getSdeptList() {
        return ResultDO.ok().put("list", sdeptService.getSdeptDOList());
    }

    @RequestMapping("get-sdepts")
    ResultDO getSdepts(@RequestParam Map<String, Object> params) {
        PageDO page = sdeptService.getSdepts(new PageQuery(params));
        return ResultDO.ok().put("page", page);
    }

    @PostMapping("save")
    ResultDO save(@RequestParam("sdeptName") String name) {
        int result = sdeptService.save(name);
        if (result == 0) {
            return ResultDO.error();
        }
        return ResultDO.ok();
    }

    @PostMapping("delete")
    ResultDO delete(@RequestBody Long[] ids) {
        int result = sdeptService.delete(ids);
        if (result == 0) {
            return ResultDO.error();
        }
        return ResultDO.ok();
    }

    @PostMapping("update")
    ResultDO update(@RequestParam("id") String id, @RequestParam("sdeptName") String name) {
        SdeptDO sdeptDO = new SdeptDO();
        sdeptDO.setId(Long.parseLong(id));
        sdeptDO.setName(name);
        int result = sdeptService.update(sdeptDO);
        if (result == 0) {
            return ResultDO.error();
        }
        return ResultDO.ok();
    }
}
    